﻿<template>
	<div class="programAll-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="queryParams" ref="queryForm" :inline="true">
			<el-row :gutter="35">
              <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6" >
              <el-form-item label="名称">
              <el-input v-model="queryParams.name" clearable placeholder="请输入名称"/>
              </el-form-item>
              </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20 search-actions">
        
          <div>
          <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
          <el-button type="primary" plain icon="ele-Search" @click="handleQuery" v-auth="'programAll:page'"> 查询 </el-button>
          </div>
          </el-col>
      </el-row>
      </el-form>
		</el-card>
		<el-card shadow="hover" style="margin-top: 8px">
			<el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
				<el-table-column type="index" label="序号" width="55" align="center" fixed="" />
				 <el-table-column prop="name" label="名称" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="runpath" label="程序部署路径" fixed="" show-overflow-tooltip="" />
				 <el-table-column  label="程序类型" fixed="" show-overflow-tooltip="" >
					<template #default="scope">
						{{ scope.row.programtype==1?"采集客户端":scope.row.programtype==2?"数据服务端":scope.row.programtype==3?"web客户端":"其它客户端" }}
					</template>
				 </el-table-column>
				 <el-table-column prop="ipaddress" label="IP端口" fixed="" show-overflow-tooltip="" />

				 <el-table-column prop="status" label="状态" fixed="" show-overflow-tooltip="" >
					<template #default="scope">
						<el-tooltip :content=scope.row.statusdes placement="top">
							<el-tag :type="scope.row.status==1 || scope.row.status==2?'success':'info'" round>{{ scope.row.status==1?"在线":scope.row.status==-1?"异常":scope.row.status==0?"离线":"运行" }}</el-tag>
							
                        </el-tooltip>
							
						
						
					</template>
				 </el-table-column>

				 <el-table-column prop="ram" label="内存" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="cpu" label="cpu" fixed="" show-overflow-tooltip="" />
				
				 <el-table-column prop="statustime" label="状态时间" fixed="" show-overflow-tooltip="" />


				 <el-table-column prop="equiptotal" label="设备" fixed="" show-overflow-tooltip="" />

				 <el-table-column prop="runtime" label="启动时间" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="message" label="信息" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="remark" label="备注" fixed="" show-overflow-tooltip="" />
				
				<el-table-column label="操作" width="240" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('programAll:control')">
					<template #default="scope">
						<el-button  size="small" text="" type="primary" @click="openEditProgramAll(scope.row)" v-auth="'programAll:control'"> 编辑 </el-button>
						<el-button v-show="scope.row.status==0" size="small" text="" type="primary" @click="delProgramAll(scope.row)" v-auth="'programAll:control'"> 删除 </el-button>
						
      &nbsp
							
							<el-dropdown >
 
								<el-button v-auth="'programAll:control'" size="small" text="" type="primary">更多</el-button>

   
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="runProgramAll(scope.row)">启动</el-dropdown-item>
		<el-dropdown-item @click="stopProgramAll(scope.row)">停止</el-dropdown-item>
		<el-dropdown-item @click="clearProgramAll(scope.row)">清理连接</el-dropdown-item>

		<el-dropdown-item v-if="scope.row.programtype==2" @click="runTask(scope.row,5)">启动扩展</el-dropdown-item>
		<el-dropdown-item v-if="scope.row.programtype==2" @click="runTask(scope.row,4)">停止扩展</el-dropdown-item>
		
      </el-dropdown-menu>
    </template>
  </el-dropdown>
						

						
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
			<editDialog
			    ref="editDialogRef"
			    :title="editProgramAllTitle"
			    @reloadTable="handleQuery"
      />
		</el-card>
	</div>
</template>

<script lang="ts" setup>
	import { ref } from "vue";
	import { ElMessageBox, ElMessage } from "element-plus";
	import { auth } from '/@/utils/authFunction';
	//import { formatDate } from '/@/utils/formatTime';

	import editDialog from '/@/views/main/programAll/component/editDialog.vue'
	import { getProgramAllPageList, deleteProgramAll,ExecuteProgram } from '/@/api/main/programAll';
import { number } from "echarts";


		const editDialogRef = ref();
		const loading = ref(false);
		const tableData = ref<any>([]);
const queryParams = ref<any>
	({});
	const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
	});
	const editProgramAllTitle = ref("");


	// 查询操作
	const handleQuery = async () => {
	loading.value = true;
	var res = await getProgramAllPageList(Object.assign(queryParams.value, tableParams.value));
	tableData.value = res.data.result?.items ?? [];
	tableParams.value.total = res.data.result?.total;
	loading.value = false;
};


// 打开编辑页面
const openEditProgramAll = (row: any) => {
	editProgramAllTitle.value = '编辑程序管理';
  editDialogRef.value.openDialog(row);
};

// 删除
const delProgramAll = (row: any) => {

	if(row.equiptotal>0){
		ElMessage.warning("程序存在绑定设备，操作失败");
		return;
	}
  ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
	    await deleteProgramAll(row);
      handleQuery();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};
// 启动
const runProgramAll = (row: any) => {
  ElMessageBox.confirm(`确定要启动吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
		let command={programId:row.id,controlType:1};
	    await ExecuteProgram(command);
      handleQuery();
      ElMessage.success("启动成功");
    })
    .catch(() => {});
};
// 启动
const clearProgramAll = (row: any) => {
  ElMessageBox.confirm(`确定要移除所有掉线设备吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
	    let command={programId:row.id,controlType:6};
	    await ExecuteProgram(command);
      handleQuery();
      ElMessage.success("清理成功");
    })
    .catch(() => {});
};

// 启动
const runTask = (row: any,type:number) => {
  ElMessageBox.confirm(`确定要执行该操作吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
		
	    let command={programId:row.id,controlType:type};
	    await ExecuteProgram(command);
      handleQuery();
      ElMessage.success("执行成功");
    })
    .catch(() => {});
};


// 停止
const stopProgramAll = (row: any) => {
  ElMessageBox.confirm(`确定要停止吗，停止后关联设备数据不会更新?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
		let command={programId:row.id,controlType:0};
	    await ExecuteProgram(command);
      handleQuery();
      ElMessage.success("停止成功");
    })
    .catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
  tableParams.value.pageSize = val;
  handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
  tableParams.value.page = val;
  handleQuery();
};


handleQuery();
</script>


